package com.al.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/login")
public class CookieLoginServlet extends HttpServlet {
	private String myUserName = "admin";
	private String myPassword = "666";
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String userName = req.getParameter("userName");
		String password = req.getParameter("password");
		if(userName.equals(myUserName) && password.equals(myPassword)) {
			Cookie cookie = new Cookie("userName", userName);
			//设置一个星期免登录
			cookie.setMaxAge(60*60*24*7);
			resp.addCookie(cookie);
			resp.sendRedirect("welcome.jsp");
		} else {
			resp.sendRedirect("login.jsp");
		}
	}
}
